package com.jxc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;

@WebServlet("/Search")
public class Search extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map wordlist = new HashMap();
        String word = req.getParameter("word");
        wordlist.put("苹果","apple");
        wordlist.put("篮球","basketball");
        wordlist.put("球","ball");
        boolean flag = wordlist.containsKey(word);
        if (flag){
            String result = (String) wordlist.get(word);
            req.setAttribute("result",result);
            req.getRequestDispatcher("success.jsp").forward(req,resp);
        }else {
            HttpSession session = req.getSession();
            session.setAttribute("result","没有查询到结果");
            resp.sendRedirect("fail.jsp");
        }
    }
}
